<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.typeName" placeholder="分类名称" clearable style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.mallId" placeholder="所属商城" clearable style="width: 150px;" class="filter-item">
        <el-option v-for="item in mallList" :key="item.key" :label="item.value" :value="item.key" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        筛选
      </el-button>
      <el-button v-waves class="filter-item" type="success" icon="el-icon-edit" @click="handleCreate">
        新增
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      max-height="690"
      highlight-current-row
      style="width: 100%;"
      :row-style="{height:'32px'}"
      :default-sort="{prop: 'typeId', order: null}"
      @sort-change="sortChange"
    >
      <el-table-column label="编号" align="center" width="80" prop="typeId" sortable="custom" fixed>
        <template slot-scope="{row}">
          <span>{{ row.typeId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分类名称" align="center">
        <template slot-scope="{row}">
          <span>{{ row.typeName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属商城" align="center" prop="mallId" sortable="custom">
        <template slot-scope="{row}">
          <el-tag size="mini" effect="dark" :type="mallTypeMap[row.mallId]"> {{ mallMap[row.mallId] }} </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="排序权重" align="center" prop="typeRank" sortable="custom">
        <template slot-scope="{row}">
          <el-tag size="mini" effect="plain" type="primary"> {{ row.typeRank }} </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="修改日期" align="center" prop="updateTime" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.updateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="260px" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button type="primary" size="mini" @click="handleRank(row)">
            排序
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row.typeId)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page-num.sync="listQuery.pageNum" :page-size.sync="listQuery.pageSize" @pagination="showTypeList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="35%">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="分类名" prop="typeName">
          <el-input v-model="temp.typeName" />
        </el-form-item>
        <el-form-item label="所属商城" prop="mallId">
          <el-select v-model="temp.mallId" clearable style="width: 200px;" class="filter-item">
            <el-option v-for="item in mallList" :key="item.key" :label="item.value" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item label="排序权重" prop="typeRank">
          <el-input-number v-model="temp.typeRank" :min="0" :step="1" step-strictly style="text-align: center" size="medium" /><p style="color: gray"> (权重数字越大，展示越靠前)</p>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>
    <el-dialog title="设置排序权重" :visible.sync="dialogRankVisible" width="225px">
      <el-input-number v-model="tempRank.typeRank" :min="0" :step="1" step-strictly style="text-align: center" />
      <p style="color: gray">(权重数字越大，展示越靠前)</p>
      <p style="color: gray"></p>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogRankVisible = false">
          取消
        </el-button>
        <el-button size="small" type="primary" @click="setRank()">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { deleteType, showTypeList, saveOrUpdateType, updateRank } from '@/api/type'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination'

export default {
  name: 'Type',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        sortProp: '',
        sortOrder: '',
        typeName: '',
        mallId: 1
      },
      temp: {
        typeId: '',
        typeName: '',
        typeRank: 0,
        mallId: '',
        createTime: ''
      },
      tempRank: {
        typeId: undefined,
        typeRank: 0
      },
      dialogFormVisible: false,
      dialogRankVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      rules: {
        typeName: [{ required: true, message: '请填写分类名', trigger: 'blur' }],
        typeRank: [{ required: true, message: '请设置排序权重', trigger: 'change' }],
        mallId: [{ required: true, message: '请选择商城', trigger: 'change' }]
      },
      mallList: [
        { key: 1, value: '爱心帮扶' },
        { key: 2, value: '特价商城' },
        { key: 3, value: '积分商城' }
      ],
      mallMap: {
        1: '爱心帮扶',
        2: '特价商城',
        3: '积分商城'
      },
      mallTypeMap: {
        1: 'danger',
        2: 'success',
        3: 'primary'
      }
    }
  },
  created() {
    this.showTypeList()
  },
  methods: {
    showTypeList() {
      this.listLoading = true
      showTypeList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.showTypeList()
    },
    sortChange(data) {
      const { prop, order } = data
      this.listQuery.sortProp = prop
      if (order === 'ascending') {
        this.listQuery.sortOrder = '1'
      } else {
        this.listQuery.sortOrder = '-1'
      }
      this.handleFilter()
    },
    resetDialog() {
      this.temp = {
        typeId: undefined,
        typeName: '',
        typeRank: 0,
        mallId: ''
      }
    },
    handleCreate() {
      this.resetDialog()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveOrUpdateType(this.temp).then(() => {
            this.showTypeList()
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: '新增商品分类成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveOrUpdateType(this.temp).then(() => {
            this.showTypeList()
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: '编辑商品分类成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleDelete(typeId) {
      this.$confirm('此操作将删除该商品分类, 分类下的所有商品将无法展示, 是否继续?', '提示', {
        confirmButtonText: '确认删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteType(typeId).then(() => {
          this.showTypeList()
          this.dialogFormVisible = false
          this.$notify({
            title: 'Success',
            message: '删除商品分类成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    },
    resetRankDialog() {
      this.tempRank = {
        typeId: undefined,
        typeRank: 0
      }
    },
    handleRank(row) {
      this.resetRankDialog()
      this.tempRank.typeId = row.typeId
      this.tempRank.typeRank = row.typeRank
      this.dialogRankVisible = true
    },
    setRank() {
      updateRank(this.tempRank).then(() => {
        this.showTypeList()
        this.dialogRankVisible = false
        this.$notify({
          title: 'Success',
          message: '设置排序权重成功',
          type: 'success',
          duration: 2000
        })
      })
    }
  }
}
</script>
